package com.github.zxhtom.excel.expose;

import com.github.zxhtom.excel.Excel;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @package com.github.zxhtom.excel.expose
 * @Class JxlsTools
 * @Description
 * @Author zhangxinhua
 * @See https://www.cnblogs.com/klguang/p/6425422.html 自定义函数
 * @Date 19-11-7 下午3:13
 */
public class JxlsTools<T> implements Excel<T> {
    @Override
    public void readData() {

    }

    @Override
    public void export(List<T> ts) {
        String parentFileName = EasyExcelTools.class.getResource("/").getPath() + "student/jxls/";
        File file = new File(parentFileName);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = parentFileName + System.currentTimeMillis() + ".xlsx";
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            InputStream is = getClass().getClassLoader().getResourceAsStream("template.xlsx");
            OutputStream os = new FileOutputStream(fileName );
            Context context = new Context();
            context.putVar("list", ts);
            context.putVar("dateFormat", dateFormat);
            JxlsHelper.getInstance().processTemplate(is, os, context);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
